﻿
using System.Drawing;
using System;
using System.Web;
using System.IO;
using EasyLift.TMS.Web.Models;

namespace EasyLift.TMS.Web.Helpers
{

    public class ImageHelper
    {
        public static Size NewImageSize(int OriginalHeight, int OriginalWidth, double FormatSize)
        {
            Size NewSize;
            double tempval;

            if (OriginalHeight > FormatSize && OriginalWidth > FormatSize)
            {
                if (OriginalHeight > OriginalWidth)
                    tempval = FormatSize / Convert.ToDouble(OriginalHeight);
                else
                    tempval = FormatSize / Convert.ToDouble(OriginalWidth);

                NewSize = new Size(Convert.ToInt32(tempval * OriginalWidth), Convert.ToInt32(tempval * OriginalHeight));
            }
            else
                NewSize = new Size(OriginalWidth, OriginalHeight); return NewSize;
        }

        /*
        public static void UploadAttachedProductImages(string id, Product Product, HttpFileCollectionBase AttachedFiles)
        {
            foreach (string inputTagName in AttachedFiles)
            {
                HttpPostedFileBase file = AttachedFiles[inputTagName];
                if (file.ContentLength > 0)
                {
                    try
                    {
                        var filename = Path.GetFileName(file.FileName);
                        string strNewFileName = id + "-" + filename;
                        string strFolderName = "Uploads";
                        string strProductImageFolderName = "ProductImages";
                        string strProductThumbnailsFolderName = "ProductThumbnails";
                        string strServerFilePath = HttpContext.Current.Server.MapPath("~") + strFolderName + "\\" + strProductImageFolderName + "\\" + strNewFileName;
                        string strServerThumbnailFilePath = HttpContext.Current.Server.MapPath("~") + strFolderName + "\\" + strProductThumbnailsFolderName + "\\" + strNewFileName;
                        string strServerRelativeFilePath = "../../" + strFolderName + "/" + strProductImageFolderName + "/" + strNewFileName;
                        string strServerRelativeThumbnailFilePath = "../../" + strFolderName + "/" + strProductThumbnailsFolderName + "/" + strNewFileName;
                        try
                        {
                            if (!string.IsNullOrEmpty(Product.SKUPictureUrl))
                            {
                                System.IO.File.Delete(Product.SKUPictureUrl.Replace("../../" + strFolderName + "/" + strProductImageFolderName + "/", HttpContext.Current.Server.MapPath("~") + strFolderName + "\\" + strProductImageFolderName + "\\"));
                            }
                            if (!string.IsNullOrEmpty(Product.SKUPictureThumbnailUrl))
                            {
                                System.IO.File.Delete(Product.SKUPictureThumbnailUrl.Replace("../../" + strFolderName + "/" + strProductThumbnailsFolderName + "/", HttpContext.Current.Server.MapPath("~") + strFolderName + "\\" + strProductThumbnailsFolderName + "\\"));
                            }
                        }
                        catch (Exception ex)
                        {
                            throw (ex);
                        }

                        file.SaveAs(strServerFilePath);

                        using (System.Drawing.Image Img = System.Drawing.Image.FromFile(strServerFilePath))
                        {
                            Size ThumbNailSize = ImageHelper.NewImageSize(Img.Height, Img.Width, 100);

                            using (System.Drawing.Image ImgThnail = new Bitmap(Img, ThumbNailSize.Width, ThumbNailSize.Height))
                            {
                                ImgThnail.Save(strServerThumbnailFilePath, Img.RawFormat);
                                ImgThnail.Dispose();
                            }
                            Img.Dispose();
                        }
                        if (!string.IsNullOrEmpty(strServerRelativeFilePath))
                        {
                            Product.SKUPictureUrl = strServerRelativeFilePath;
                        }
                        if (!string.IsNullOrEmpty(strServerRelativeThumbnailFilePath))
                        {
                            Product.SKUPictureThumbnailUrl = strServerRelativeThumbnailFilePath;
                        }

                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                }
            }
        }
        */
    }
}